package sk.ogame.realm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sk.ogame.entity.PlanetOverview;
import sk.ogame.realm.service.PlanetService;

import java.time.LocalTime;
import java.util.List;

/**
 * @author 支绍昆
 * 2023/2/1
 * 首页
 **/
@Controller
public class HomeController {

    private PlanetService planetService;

    public HomeController(PlanetService planetService){
        this.planetService = planetService;
    }

    //http://localhost:8080/home
    @GetMapping({"/", "/home"})
    public String home(@RequestParam(name = "name", required = false, defaultValue = "Star") String name, Model model) {
        model.addAttribute("name", name);
        return "home";
    }

    @ResponseBody
    @GetMapping("/home/flash/star")
    public String flashStar() throws InterruptedException {
        List<PlanetOverview> overviewList = planetService.planetOverviewList();
        if (CollectionUtils.isEmpty(overviewList)){
            return "no star";
        }
        for (PlanetOverview ov : overviewList){
            Thread.sleep(3000L);
            planetService.change(ov.getPid());
        }
        return LocalTime.now().toString();
    }

}
